package com.yzx.lifeassistants.model.analysis;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yzx.lifeassistants.bean.MyBook;
import com.yzx.lifeassistants.utils.LogcatUtils;

/**
 * @Description: 解析登录图书馆返回的结果
 * @author: yzx
 * @time: 2016-3-9 下午8:38:15
 */
public class LoginLibraryAnalysis {
	/**
	 * 
	 * @Description: 是否登录失败
	 */
	public static Boolean isLoginFailed(String response) {
		Boolean isLoginFailed = true;
		try {
			JSONObject object = new JSONObject(response);
			if (object.getBoolean("status")) {
				isLoginFailed = false;
			}
		} catch (JSONException e) {
			if (null != e.getMessage()) {
				LogcatUtils.e("解析登录图书馆返回的结果异常", e.getMessage());
			}
		}
		return isLoginFailed;
	}

	/**
	 * 
	 * @Description: 是否未借阅图书
	 */
	public static Boolean isMyBookNull(String response) {
		Boolean isMyBookNull = true;
		try {
			JSONObject object = new JSONObject(response);
			if (object.getBoolean("status")) {
				if (object.getJSONArray("data").length() > 0) {
					isMyBookNull = false;
				}
			}
		} catch (JSONException e) {
			if (null != e.getMessage()) {
				LogcatUtils.e("解析登录图书馆返回的结果异常", e.getMessage());
			}
		}
		return isMyBookNull;
	}

	public static List<MyBook> getMyBooks(String response) {
		List<MyBook> myBooks = new ArrayList<MyBook>();
		try {
			JSONObject jsonObject = new JSONObject(response);
			if (jsonObject.getBoolean("status")) {
				JSONArray array = jsonObject.getJSONArray("data");
				for (int i = 0; i < array.length(); i++) {
					JSONObject object = array.getJSONObject(i);
					MyBook myBook = new MyBook();
					myBook.setBookName(object.getString("bookName"));
					myBook.setBarcode(object.getString("barcode"));
					myBook.setReturnTime(object.getString("returnTime"));
					myBooks.add(myBook);
				}
			}
		} catch (JSONException e) {
			if (null != e.getMessage()) {
				LogcatUtils.e("解析登录图书馆返回的结果异常", e.getMessage());
			}
		}
		return myBooks;
	}
}
